﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;
using System.Linq;

namespace LCA.Community.DAL.Test
{
    /// <summary>
    ///This is a test class for HelpRelationshipsRepositoryTest and is intended
    ///to contain all HelpRelationshipsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class HelpRelationshipsRepositoryTest : BaseTestClass
    {
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        private HelpRelationshipFactory _helpRelationshipFactory = new HelpRelationshipFactory();
        private List<User> _helpedUsers;
        private List<User> _volunteers;
        private List<HelpRelationship> _helpRealtionships;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(10);
            _users.ForEach(user => usersRepository.Add(user));

            var helpRelationshipRepository = _unityContainer.Resolve<IHelpRelationshipsRepository>();
            HelpRelationship hr;
            _helpedUsers = new List<User>();
            _volunteers = new List<User>();
            _helpRealtionships = new List<HelpRelationship>();

            hr = _helpRelationshipFactory.CreateEntity(_users[0].RowKey, _users[1].RowKey);
            _volunteers.Add(_users[0]);
            _helpedUsers.Add(_users[1]);
            helpRelationshipRepository.Add(hr);
            _helpRealtionships.Add(hr);

            hr = _helpRelationshipFactory.CreateEntity(_users[3].RowKey, _users[1].RowKey);
            _volunteers.Add(_users[3]);
            helpRelationshipRepository.Add(hr);
            _helpRealtionships.Add(hr);

            hr = _helpRelationshipFactory.CreateEntity(_users[0].RowKey, _users[2].RowKey);
            _helpedUsers.Add(_users[2]);
            helpRelationshipRepository.Add(hr);
            _helpRealtionships.Add(hr);

            hr = _helpRelationshipFactory.CreateEntity(_users[3].RowKey, _users[4].RowKey);
            _volunteers.Add(_users[3]);
            _helpedUsers.Add(_users[4]);
            helpRelationshipRepository.Add(hr);
            _helpRealtionships.Add(hr);
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var helpRelationshipRepository = _unityContainer.Resolve<IHelpRelationshipsRepository>();
            usersRepository.DeleteTestEntities();
            helpRelationshipRepository.DeleteTestEntities();

        }

        /// <summary>
        ///A test for DeleteAllUserHelpRelationships
        ///</summary>
        [TestMethod()]
        public void DeleteAllUserHelpRelationshipsTest()
        {
            var target = _unityContainer.Resolve<IHelpRelationshipsRepository>();
            string userID = _users[0].RowKey;

            target.DeleteAllUserHelpRelationships(userID);

            var actual = target.GetHelpRelationshipsByUser(userID);
            CollectionAssert.AreEquivalent(new List<HelpRelationship>(), actual);
        }

        /// <summary>
        ///A test for GetHelpRelationshipsByUser
        ///</summary>
        [TestMethod()]
        public void GetHelpRelationshipsByUserTest()
        {
            var target = _unityContainer.Resolve<IHelpRelationshipsRepository>();
            string userID = _users[0].RowKey;

            List<HelpRelationship> expected = _helpRealtionships.Where(hr => hr.Volunteer == userID || hr.User == userID).ToList();

            List<HelpRelationship> actual;
            actual = target.GetHelpRelationshipsByUser(userID);

            CollectionAssert.AreEquivalent(expected, actual);
        }
    }
}
